import groovy.jmx.builder.modelmbean.ModelMBeanBuilder
import groovy.jmx.builder.node.MBeanNodeData

class ModelMBeanBuilderTest extends GroovyTestCase {
    def dataGenerator
    def builder

    void setUp() {
        dataGenerator = new NodeDataGenerator()
        builder = new ModelMBeanBuilder()
    }

    void testBuildModelMBeanFromNodeData() {
        def treeNode = dataGenerator.generateMBeanNodeData()
        assert treeNode != null

        def mbean = builder.build(treeNode)
        assert mbean != null

        def info = mbean.getMBeanInfo()
        assert info != null
        assert info.getConstructor("ExposedTestBean") != null

        assert info.getAttribute("Color") != null
        assert info.getAttribute("Shape") != null
        assert info.getAttribute("Length") != null
        assert info.getAttribute("AccessPort") != null

        assert info.getOperation("stretch") != null
    }

    void testBuildModelMBeanFromInstance() {
        def data = new MBeanNodeData(object: new ExposedTestBean())
        def mbean = builder.build(data)
        assert mbean != null

        def info = mbean.getMBeanInfo()
        assert info != null

        assert info.getConstructor("ExposedTestBean") != null

        assert info.getAttribute("Id") != null
        assert info.getAttribute("Color") != null
        assert info.getAttribute("Shape") != null
        assert info.getAttribute("Length") != null
        assert info.getAttribute("AccessPort") != null

        assert info.getOperation("toss") != null
        assert info.getOperation("stretch") != null
        assert info.getOperation("receive") != null
    }

    void testBuildModelMBeanFromJavaInstance() {
        def mbean = builder.build(new MBeanNodeData(object: new TestJavaBean()))
        assert mbean != null

        def info = mbean.getMBeanInfo()
        assert info != null

        assert info.getConstructor("TestJavaBean") != null

        assert info.getAttribute("StringProperty") != null
        assert info.getAttribute("BoolProperty") != null
        assert info.getAttribute("IntProperty") != null

        assert info.getOperation("create") != null
        assert info.getOperation("destroy") != null
    }

}